﻿Shader "Unlit/netEasePBR"
{
	Properties
	{
		AlphaMtl("AlphaMtl", Float) = 1.0
		metal_multi("metal_multi", Float) = 0.0
		rough_multi("rough_multi", Float) = 0.0
		diffuse_intensity("diffuse_intensity", Float) = 1
		u_light_scale("u_light_scale", Float) = 1
		sss_factor("sss_factor", Float) = 0.2
		env_shadow_factor("env_shadow_factor", Vector) = (0.667, 0.545, 0.761, 1)
		envir_brightness("envir_brightness", Float) = 1
		min_ggx_roughness("min_ggx_roughness", Float) = 0.04
		max_ggx_roughness("max_ggx_roughness", Float) = 1
		noise_offset("法线随机扰动噪声", Float) = 0.48
		normal_offset("法线受4_6贴图影响偏移系数", Float) = 0.1
		emissive_bloom("自发光系数", Float) = 1 
		illum_multi("illum_multi", Float) = 1
		bloom_range("bloom_range", Float) = 0.4
		u_tonemapping_factor("u_tonemapping_factor", Float) = 1
		force_pixel_color("force_pixel_color", Color) = (0, 0, 0, 0)
		AO_slider("AO_slider", Float) = 1
		max_brightness("max_brightness", Float) = 130
		envir_fresnel_brightness("envir_fresnel_brightness", Float) = 0.35
		sam_diffuse_0 ("diffuse", 2D) = "white" {}
		sam_other1_2 ("change color", 2D) = "white" {}
		sam_other2_3("envirnoment", 2D) = "white" {}
		sam_other3_5 ("xnormalmap", 2D) = "white" {}
		sam_other4_6 ("aniso", 2D) = "white" {}
		
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" "LightMode" = "ForwardBase" }
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma multi_compile_fwdbase
			
			#include "UnityCG.cginc"
			#include "AutoLight.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float3 normal: NORMAL;
				float4 tangent: TANGENT;
				float2 texcoord : TEXCOORD0;
			};

			struct v2f
			{
				float4 pos : SV_POSITION;
				float3 normalWorld: NORMAL;

				float2 texcoord0 : TEXCOORD0;
				float4 texcoord2 : TEXCOORD2;
				float3 texcoord4 : TEXCOORD4;
				float3 texcoord5 : TEXCOORD5;

				SHADOW_COORDS(3)

			};

			sampler2D sam_diffuse_0;
			sampler2D sam_other1_2;
			sampler2D sam_other2_3;
			sampler2D sam_other3_5;
			sampler2D sam_other4_6;
			
			v2f vert (appdata v)
			{
				v2f o;
				o.pos = UnityObjectToClipPos(v.vertex);
				o.normalWorld = UnityObjectToWorldNormal(v.normal);//normal
				o.texcoord0 = v.texcoord;//uv
				o.texcoord2 = float4(mul(unity_ObjectToWorld, v.vertex).xyz, 1);//worldPosition
				o.texcoord4 = normalize(mul(unity_ObjectToWorld, v.tangent).xyz);//tangent
				o.texcoord5 = normalize(cross(o.texcoord4, o.normalWorld));//bitangent
				TRANSFER_SHADOW(o);
				return o;
			}
			

			fixed metal_multi;
			fixed rough_multi;
			half diffuse_intensity;
			half u_light_scale;
			half sss_factor;
			fixed4 env_shadow_factor;
			half envir_brightness;
			fixed min_ggx_roughness;
			fixed max_ggx_roughness;
			fixed noise_offset;
			fixed normal_offset;
			fixed emissive_bloom;
			half illum_multi;
			fixed bloom_range;
			fixed u_tonemapping_factor;
			fixed3 force_pixel_color;
			float AlphaMtl;
			fixed AO_slider;
			half max_brightness;
			half envir_fresnel_brightness;

			fixed4 frag (v2f i) : SV_Target
			{
				//EYE_ENABLE
				float local_0 = 1.0;

				//XNORMAL_MAP_ENABLE
				fixed3 normal;
				fixed local_4;
				fixed3 bitangent;
				fixed3 tagent;
				fixed2 local_7;
				fixed local_8;
				half2 local_9 = i.texcoord0;
				fixed4 local_11 = tex2D(sam_other3_5, local_9); //bias
				fixed2 local_12 = local_11.xy;
				fixed2 local_15 = local_12 * 2.0;
				fixed2 local_18 = local_15 - fixed2(1.0, 1.0);
				fixed3 local_21 = i.texcoord4 * local_18.x;
				fixed3 local_23 = i.texcoord5 * local_18.y;
				fixed3 local_25 = local_21 + local_23;
				fixed3 local_26 = local_25 + i.normalWorld;
				normal = normalize(local_26);//normalMap normal in world
				local_4 = local_11.z;
				bitangent = i.texcoord5;
				tagent = i.texcoord4;
				local_7 = local_18;
				local_8 = local_11.z;

				//EYE_ENABLE
				fixed3 local_36 = normal;//normal

				//SHADOW_MAP_ENABLE
				fixed shadow = SHADOW_ATTENUATION(i);//shadow attenuation

				fixed4 diffuse = tex2D(sam_diffuse_0, i.texcoord0);//diffuse

				//SEPARATE_ALPHA_TEX
				//fixed4 local_96 = diffuse;

				//return diffuse;

				fixed4 local_103 = tex2D(sam_other1_2, i.texcoord0); //x: metal, y: sss related, z: roughness

				//CHANGECOLOR_ENABLE no
				//fixed4 local_108 = local_96;
				fixed4 local_109 = local_103;

				//MANUL_ENABLE no
				fixed metal = clamp(local_109.x + metal_multi, 0.0, 1.0);//metal
				fixed roughness = clamp(local_109.z + rough_multi, 0.0, 1.0);//roughness
				fixed sss = clamp(2.0 * local_109.y - 1.0, 0.0, 1.0);//sam_other1_2.y (-1, 1) sss related factor 

				fixed3 lightDir = -normalize(_WorldSpaceLightPos0.xyz); //light direction
				half3 worldPos = i.texcoord2.xyz;
				fixed3 camToWorldPos = normalize(_WorldSpaceCameraPos - worldPos); // camera to worldPos
				fixed3 local_274 = lightDir * u_light_scale;
				fixed final_metal = 1.0 - metal;
				fixed3 H = normalize(camToWorldPos - lightDir);//view-light halfway dir
				fixed VDotH = clamp(dot(camToWorldPos, H), 0.0, 1.0);
				fixed NDotH = max(0.0, dot(normal, H));
				fixed NDotV = clamp(dot(camToWorldPos, normal), 0.0, 1.0);
				fixed NDotL = -dot(normal, local_274);

				//SSS yes
				half local_287;
				fixed local_290 = NDotL + 0.45;
				fixed local_291 = max(0.0, local_290);
				fixed local_294 = local_291 / 1.45;
				fixed local_295 = min(local_294, shadow);
				fixed local_296 = local_295 * local_295;
				fixed NDotLClamped = clamp(NDotL, 0.0, 1.0);
				half NDotLClampedWithShadow = min(NDotLClamped, shadow);
				half local_299 = sss * sss_factor;
				local_287 = lerp(NDotLClampedWithShadow, local_296, local_299);
				
				half local_304 = clamp(local_287, -1.0, 0.0);
				//EYE_ENABLE
				half local_305 = local_287;

				fixed3 local_313 = diffuse.xyz;
				fixed3 local_315 = local_313 * local_313;
				half3 local_316 = local_315 * diffuse_intensity;
				half3 local_317 = final_metal * local_316;

				////DIR_AMBIENT_ENABLE no
				half3 local_318 = half3(0.0, 0.0, 0.0);

				//ENVIR_ENABLE yes
				fixed local_341 = lerp(1.0, local_4, NDotV);
				fixed local_344 = 0.5 - local_4;
				fixed local_345 = clamp(local_344, 0.0, 1.0);
				fixed local_346 = 2.0 * local_345;
				fixed local_347 = local_346 * AO_slider;
				fixed local_348 = lerp(local_341, local_4, local_347);
				fixed3 local_339 = fixed3(local_348, local_348, local_348);


				fixed local_357 = max(max(diffuse.x, diffuse.y), diffuse.z);
				fixed local_359 = local_357 - 0.39;
				fixed3 local_360 = fixed3(local_359, local_359, local_359);
				fixed3 local_362 = fixed3(0.1, 0.1, 0.1);
				fixed3 local_363 = max(local_360, local_362);
				fixed3 local_364 = local_313 - local_363;
				fixed3 local_365 = clamp(local_364, 0.0, 1.0);
				
				//EYE_ENABLE no
				fixed3 local_366 = local_339;

				//SSS_ENABLE yes
				fixed3 local_369;
				fixed3 local_370;
				fixed local_373 = clamp(-NDotL, 0.0, 1.0);
				fixed local_375 = clamp(NDotL, 0.0, 1.0);
				fixed local_377 = lerp(local_375, local_373, 0.61);
				fixed local_378 = 1.0 - local_377;
				fixed local_381 = -0.47 + local_378;
				half local_384 = local_381 / 0.53;
				half local_386 = local_304 + local_287;
				half local_388 = local_386 / 2.0;
				fixed local_389 = smoothstep(0.51, 0.0, local_388);
				half local_390 = clamp(local_384, 0.0, 1.0);
				half local_391 = local_390 * local_389;
				half local_392 = local_391 * sss;
				half local_393 = local_392 * sss_factor;
				half3 local_394 = local_393 * local_365;
				half3 local_396 = local_394 * 2.35;
				local_369 = local_396 * 0.3;
				fixed local_399 = 2.0 - NDotV;
				fixed3 local_400 = local_399 * local_365;
				fixed3 local_401 = local_400 * sss;
				local_370 = local_401 * sss_factor;

				//ENVIR_ENABLE yes
				fixed3 local_407 = -lightDir;
				fixed3 local_406 = reflect(local_407, normal);

				half3 envShColor;
				// Function calc_env_sh Begin 
				{
					envShColor = ShadeSH9(half4(normal, 1.0));
				}
				// Function calc_env_sh End

				fixed3 local_423 = env_shadow_factor.xyz;
				fixed3 local_425 = fixed3(1.0, 1.0, 1.0);
				half3 local_426 = half3(local_305, local_305, local_305);
				fixed3 local_427 = lerp(local_423, local_425, local_426);
				half3 local_430 = local_426 + local_369;
				half3 local_431 = local_427 * envShColor;
				half3 local_432 = envShColor * local_370;
				half3 local_433 = local_431 + local_432;
				half3 local_434 = local_433 * envir_brightness;
				half3 local_435 = local_430 + local_434;
				half3 local_436 = local_317 * local_435;
				half3 local_437 = local_436 + local_318;

				float3 local_439 = float3(0.04, 0.04, 0.04);
				fixed3 local_440 = fixed3(final_metal, final_metal, final_metal);
				fixed3 local_441 = lerp(local_316, local_439, local_440);
				fixed final_roughness = lerp(min_ggx_roughness, max_ggx_roughness, roughness);

				//ANISO_ENABLE yes
				fixed local_443;
				fixed4 local_444 = tex2D(sam_other4_6, i.texcoord0);
				float4 local_446 = float4(0.5, 0.5, 0.5, 0.5);
				fixed4 local_447 = local_444 - local_446;
				fixed4 local_449 = local_447 * 2.0;
				fixed3 local_454 = local_449.x * tagent;
				fixed3 local_455 = local_449.y * bitangent;
				fixed3 local_457 = normalize(local_454 + local_455);
				fixed local_461 = local_444.z - 0.5;
				fixed local_462 = local_461 * noise_offset;
				half local_463 = local_462 + normal_offset;
				fixed3 local_464 = normal * local_463;
				fixed3 local_466 = normalize(local_464 + local_457);
				fixed local_467 = dot(local_466, camToWorldPos);
				fixed local_468 = clamp(local_467, 0.0, 1.0);
				fixed local_471 = sin(local_468 * 3.141593);
				fixed local_473 = max(0.0, local_471);
				fixed local_475 = local_109.y;
				fixed local_478 = step(local_475, 0.25);
				local_443 = lerp(NDotH, local_473, local_478);

				// Function F_Schlick Begin 
				half3 local_487;
				{
					fixed local_490 = 1.0 - VDotH;
					fixed local_492;
					// Function SG Begin 
					{
						half local_495 = 1.442695 * 5.0;
						half local_497 = local_495 + 1.089235;
						half local_498 = local_497 * local_490;
						half local_499 = local_498 - local_497;
						local_492 = exp2(local_499);
					}
					// Function SG End
					float3 local_502 = float3(1.0, 1.0, 1.0);
					half3 local_503 = local_502 - local_441;
					half3 local_504 = local_503 * local_492;
					local_487 = local_441 + local_504;
				}
				// Function F_Schlick End

				// Function D_GGX Begin 
				float local_507;
				{
					fixed local_509 = final_roughness * final_roughness;
					fixed local_510 = local_509 * local_509;
					fixed local_511 = local_443 * local_510;
					fixed local_512 = local_511 - local_443;
					fixed local_513 = local_512 * local_443;
					fixed local_515 = local_513 + 1.0;
					fixed local_516 = local_515 * local_515;
					fixed local_517 = local_510 / local_516;
					local_507 = min(local_517, 10000.0);
				}
				// Function D_GGX End

				// Function G_Schlick_Disney Begin 
				half local_521;
				{
					fixed local_524 = 0.5 * final_roughness;
					fixed local_525 = 0.5 + local_524;
					fixed local_526 = 0.5 * final_roughness;
					fixed local_527 = 0.5 + local_526;
					fixed local_528 = local_525 * local_527;
					fixed local_530 = 1.0 - local_528;
					fixed local_531 = NDotV * local_530;
					fixed local_532 = local_531 + local_528;
					fixed local_533 = 1.0 - local_528;
					fixed local_534 = local_287 * local_533;
					fixed local_535 = local_534 + local_528;
					fixed local_537 = local_532 * local_535;
					local_521 = 0.25 / local_537;
				}
				// Function G_Schlick_Disney End

				float3 local_540 = local_507 * local_487;
				half3 local_541 = local_540 * local_521;
				half3 local_542 = local_541 * local_287;

				//ENVIR_ENABLE yes
				half3 local_543;
				// Function GetEnvirmentColor_panorama Begin
				half3 local_544;
				{
					float local_547 = roughness / 0.14;
					float2 local_551 = local_406.xy;
					float local_552 = local_406.z;
					float local_553 = local_406.x;
					float local_554 = local_406.y;
					float local_555 = local_406.z;
					half local_556 = atan2(local_552, local_553);
					half local_557 = local_556 + 3.141593;
					half local_558 = local_557 * 0.15915949;
					float local_559 = acos(local_554);
					float local_560 = local_559 * 0.3183099;
					half4 local_561 = half4(local_558, local_560, 0, local_547);
					half4 local_562 = tex2Dlod(sam_other2_3, local_561);
					half3 local_563 = local_562.xyz;
					half local_564 = local_562.w;
					half3 local_565 = local_563 * local_564;
					local_544 = local_565 * max_brightness;
				}
				// Function GetEnvirmentColor_panorama End

				// Function EnvBRDFApprox Begin 
				half3 local_568;
				{
					fixed4 local_577 = fixed4(-1.0, -0.0275, -0.572, 0.022);
					fixed4 local_582 = fixed4(1.0, 0.0425, 1.04, -0.04);
					fixed4 local_583 = roughness * local_577;
					half4 local_584 = local_583 + local_582;
					half local_585 = local_584.x;
					half local_586 = local_584.y;
					half local_587 = local_584.z;
					half local_588 = local_584.w;
					half local_589 = local_585 * local_585;
					fixed local_592 = -9.28 * NDotV;
					fixed local_593 = exp2(local_592);
					half local_594 = min(local_589, local_593);
					half local_595 = local_594 * local_585;
					half local_596 = local_595 + local_586;
					float2 local_598 = float2(-1.04, -1.04);
					half2 local_599 = local_598 * local_596;
					half2 local_600 = local_584.xy;
					half2 local_601 = local_584.zw;
					half2 local_602 = local_599 + local_601;
					half local_603 = local_602.x;
					half local_604 = local_602.y;
					half3 local_605 = local_441 * local_603;
					half3 local_606 = half3(local_604, local_604, local_604);
					half3 local_607 = local_606 * envir_fresnel_brightness;
					local_568 = local_605 + local_607;
				}
				// Function EnvBRDFApprox End

				half3 local_610 = envir_brightness * local_568;
				half3 local_611 = local_610 * local_544;
				local_543 = local_611 * local_427;

				//SELF_BLING no
				half3 local_619 = half3(0.0, 0.0, 0.0);
				fixed local_620 = 0.0;

				half local_631 = lerp(0.6, 1.0, local_287);
				half3 local_632 = local_543 * local_631;
				half3 local_633 = local_542 + local_632;
				half3 local_634 = local_437 + local_633;
				half3 local_635 = local_634 * local_366;
				half3 local_636 = local_635 + local_619;

				//FRESNEL_ENABLE
				half3 local_637 = local_636;
				fixed local_638 = 1.0;

				half3 local_664 = local_637 + force_pixel_color;

				//DIFFUSE_CHECK
				half3 local_665 = local_664;

				fixed local_666 = bloom_range + illum_multi;
				float3 local_668 = float3(0.187, 0.187, 0.187);
				half3 local_669 = local_665 + local_668;
				half3 local_670 = local_665 / local_669;
				half3 local_672 = local_670 * 1.035;
				fixed local_674 = local_638 * AlphaMtl;
				fixed4 local_675 = fixed4(local_672.x, local_672.y, local_672.z, local_674);

				// Function Luminance Begin 
				half local_676;
				{
					float3 local_681 = float3(0.3, 0.59, 0.11);
					local_676 = dot(local_633, local_681);
				}
				// Function Luminance End

				half3 local_684 = sqrt(local_665);
				float3 local_686 = float3(1.5, 1.5, 1.5);
				half3 local_687 = local_684 / local_686;
				fixed local_688 = metal + bloom_range;
				fixed local_689 = clamp(local_688, 0.0, 1.0);
				half local_690 = local_676 * local_689;
				half local_691 = local_690 * illum_multi;
				fixed local_692 = local_620 * emissive_bloom;
				half local_693 = local_691 + local_692;
				fixed4 local_694 = fixed4(local_687.x, local_687.y, local_687.z, local_693);
				fixed4 local_695 = fixed4(u_tonemapping_factor, u_tonemapping_factor, u_tonemapping_factor, u_tonemapping_factor);
				fixed4 local_696 = lerp(local_694, local_675, local_695);

				//ROUGHNESS_CHECK
				fixed4 local_697 = local_696;

				//METAL_CHECK
				fixed4 local_700 = local_697;

				//AO_CHECK
				fixed4 local_703 = local_700;

				return local_703;
			}
			ENDCG
		}

		Pass
		{
			Name "ShadowCaster"
			Tags{ "LightMode" = "ShadowCaster" }

			ZWrite On ZTest LEqual
			CGPROGRAM
			#pragma multi_compile_shadowcaster

			
			#pragma vertex vertShadowCaster
			#pragma fragment fragShadowCaster

			#include "UnityStandardShadow.cginc"	
			ENDCG
		}
	}

	Fallback "VertexLit"
}
